package net.zjitc.controller.client;

import net.zjitc.common.ServiceResp;
import net.zjitc.pojo.*;
import net.zjitc.service.CategoryService;
import net.zjitc.service.OrderItemService;
import net.zjitc.service.OrderService;
import net.zjitc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/24 13:59
 */
@Controller
public class MyController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    OrderService orderService;
    @Autowired
    OrderItemService orderItemService;
    @Autowired
    ProductService productService;
    public void top(Model model){
        List<Category> list = categoryService.findAll();
        model.addAttribute("list",list);
    }


    @RequestMapping(value = "/myAccount")
    public String myAccount(Model model){
        top(model);
        return "/client/myAccount.jsp";
    }

    @RequestMapping(value = "/modifyuserinfo")
    public String modifyuserinfo(Model model){
        top(model);
        return  "/client/modifyuserinfo.jsp";
    }

    @RequestMapping(value = "/findOrderByUser")
    public String findOrderByUser(Model model, HttpSession session){
        top(model);
        User user = (User) session.getAttribute("user");
        List<Order> orderList= orderService.findOrderByUser(user.getId());
        model.addAttribute("orders",orderList);
        return "/client/orderlist.jsp";
    }

    @RequestMapping(value = "/findOrderById")
    public String findOrderById(Model model,String id){
        top(model);
        Order order = orderService.findById(id);
        List<OrderItem> orderItemList = orderItemService.findByOId(id);
        for (OrderItem o:orderItemList
             ) {
            Products products = productService.findById(o.getPid());
            o.setProducts(products);
        }
        order.setOrderItemList(orderItemList);
        model.addAttribute("order",order);
        return "/client/orderInfo.jsp";
    }

    @RequestMapping(value = "/payui")
    public String payui(){
        return "/client/pay.jsp";
    }

    @RequestMapping(value = "/pay")
    public String pay(String id,Model model){
        top(model);
        orderService.update_state(id);
        return "/client/paysuccess.jsp";
    }

    @RequestMapping(value = "/delOrderById")
    public String delOrderById(String id,String type) {
        orderService.delete(id);
        return "redirect:/findOrderByUser";
    }

}
